package com.demo.thread;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

public class TimeTest {
    private static AtomicInteger getWay502 = new AtomicInteger(0);
    private static int SERVERERRO_RETRY_MAXNUM = 2;

    private static long getWay502StopTimeInMs = 1 * 60  * 1000;
    /**
     * 判断当前时间是否在某个时间内
     * @param nowDate
     * @param startDate
     * @param endDate
     * @return
     * @throws Exception
     */
    public static boolean hourMinuteBetween(String nowDate, String startDate, String endDate) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        Date now = format.parse(nowDate);
        Date start = format.parse(startDate);
        Date end = format.parse(endDate);
        long nowTime = now.getTime();
        long startTime = start.getTime();
        long endTime = end.getTime();
        return nowTime >= startTime && nowTime <= endTime;
    }

    public static void main(String[] args) throws Exception {
//        try {
//            System.out.println(hourMinuteBetween("18:38","19:00","20:00"));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        testStop();
        testStop();
    }

    private static  void testStop () throws  Exception{
        int count = getWay502.incrementAndGet();
        System.out.println(new Date()+"开始testStop：count"+count);
        if(count >= SERVERERRO_RETRY_MAXNUM){
            System.out.println("进入了断路器==="+count);
            long sleepStart = new Date().getTime();
            for(;;){
                Thread.sleep(1000);
               // System.out.println("服务死了！！！！！！！！！！！！！！！！！！");
                long passInMs = new Date().getTime();
                long pass = passInMs - sleepStart;
                if(pass > getWay502StopTimeInMs){
                    System.out.println("超过停顿的时间，自己开始恢复数据");
                    getWay502.set(0);
                    testStop();
                    break;
                }
            }
        }else{
            System.out.println(new Date()+"重新开始走"+getWay502.get());
        }
    }



}
